package com.health.es.temp;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author nqlz
 */
public class CollStreamKit extends CollStreamUtil {
    public static <E> List<E> toFilter(Collection<E> collection, Predicate<E> filter) {
        if (CollUtil.isEmpty(collection)) {
            return Collections.emptyList();
        }
        return collection
                .stream()
                .filter(filter)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    public static <E, T> List<T> toListParallel(Collection<E> collection, Function<E, T> function) {
        if (CollUtil.isEmpty(collection)) {
            return Collections.emptyList();
        }
        return collection
                .parallelStream()
                .map(function)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    public static <E, K, V> Map<K, V> toMap(Collection<E> collection, Function<E, K> key, Function<E, V> value, BinaryOperator<V> mergeFunction) {
        if (CollUtil.isEmpty(collection)) {
            return Collections.emptyMap();
        }
        return collection.parallelStream().collect(Collectors.toMap(key, value, mergeFunction));
    }

}
